const app = getApp()

Page({
  data: {
    isGetUserInfo:false,

    tabbar: {},
    imagesLists: [],
    page: 1
  },

  onLoad: function() {
    var that = this
    wx.hideTabBar();

    wx.showLoading({
      title: '加载中',
    });

    app.getAccInfo().then(function(){
      wx.getUserInfo({
        success(res) {
          wx.hideLoading()
          var userInfo = res.userInfo
            if(app.globalData.openId){
              wx.request({
                url: app.globalData.url + 'update_acc_info',
                data: {
                  "openid": app.globalData.openId,
                  "nickname": userInfo.nickName,
                  "avatar": userInfo.avatarUrl
                },
                method: 'POST',
                success:(res)=>{
                  if(res.data.data == 1){
                    that.setData({
                      isGetUserInfo:true
                    })
                  }
                  wx.setStorage({
                    key: 'MagicPlateUserInfo',
                    data: userInfo,
                    success: (result)=>{}
                  });
                }
              });
              that.setData({
                isGetUserInfo:true
              })
            }          
        },
        fail(){
          that.setData({
            isGetUserInfo:false
          })
        },
      })
      
      app.editTabbar();
      // 获取图片
      that.getListsByPage()
    })
    
  },


  // 页面下拉 ------ 随机获取图片
  onPullDownRefresh: function() {
    this.getRandomList()
  },


  // 页面触底 ------ 加载第二页图片
  onReachBottom: function() {
    wx.showLoading({
      title: '加载数据中...',
      success: this.getListsByPage()
    })
  },

  onShareAppMessage: function() {
    return {
      title: '创作属于你的艺术!',
      imageUrl: '/images/share.jpg',
      path: '/pages/index/index'
    }
  },

  // 获取用户信息
  getuserinfo:function(e){
    var that = this
    var userInfo = e.detail.userInfo
    app.globalData.userInfo = userInfo
    wx.request({
      url: app.globalData.url + 'update_acc_info',
      data: {
        "openid": app.globalData.openId,
        "nickname": e.detail.userInfo.nickName,
        "avatar": e.detail.userInfo.avatarUrl
      },
      method: 'POST',
      success:(res)=>{
        if(res.data.data == 1){
          that.setData({
            isGetUserInfo:true
          })
        }
        wx.setStorage({
          key: 'MagicPlateUserInfo',
          data: userInfo,
          success: (result)=>{}
        });
      }
    });
  },

  // 跳转详情页
  toDetail: function(e) {
    var user = {
      id: e.currentTarget.dataset.id,
      imgUrl: e.currentTarget.dataset.img,
      userImg: e.currentTarget.dataset.userimg,
      userName: e.currentTarget.dataset.username,
    }

    wx.navigateTo({
      url: '/pages/detail/detail?user=' + JSON.stringify(user),
    });
  },

  // 获取随机列表
  getRandomList: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'get_ramdom_list',
      success: (res) => {
        
        if (res.data.data) {
          that.setData({
            imagesLists: res.data.data,
            page: 1
          })
          wx.hideLoading()
          wx.hideNavigationBarLoading()
          wx.stopPullDownRefresh()
          wx.showToast({
            title: '已是最新',
            icon: 'success',
            duration: 1000
          })
        }
      }
    });
  },

  // 获取分页数据
  getListsByPage: function() {
    var that = this
    var page = this.data.page
    var imagesLists = this.data.imagesLists
    wx.request({
      url: app.globalData.url + 'get_item_list',
      data:{
        'v': app.globalData.version,
        'page': page
      },
      method: 'POST',
      success: (res) => {
        var list = res.data.data
        if (list && list.length != 0) {
          that.setData({
            page: page + 1,
            imagesLists: imagesLists.concat(list),
          })
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '已经到底了',
            icon: 'success',
            duration: 1000
          })
        }

      }
    });
  }
})